export const getQueryString = (name) => {
  var reg = new RegExp('([&,?])' + name + '=([^&]*)(&|$)', 'i')
  var r = window.location.search.match(reg) || window.location.hash.match(reg)
  if (r != null) return decodeURIComponent(r[2])
  return null
}

export function localStor(key, val) {
  if (typeof key !== 'string') throw new Error('must be a string')
  if (val === undefined) {
    try {
      return JSON.parse(localStorage.getItem(key))
    } catch (error) {
      return localStorage.getItem(key)
    }
  } else {
    val = typeof val === 'string' ? val : JSON.stringify(val)
    localStorage.setItem(key, val)
  }
}

export function sessionStor(key, val) {
  if (typeof key !== 'string') throw new Error('must be a string')
  if (val === undefined) {
    try {
      return JSON.parse(sessionStorage.getItem(key))
    } catch (error) {
      return sessionStorage.getItem(key)
    }
  } else {
    val = typeof val === 'string' ? val : JSON.stringify(val)
    sessionStorage.setItem(key, val)
  }
}
